home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 13.5 KB | 465 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWPrvRFl.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWPRVRFL_H
- #include "FWPrvRFl.h"
- #endif
-
- #ifndef FWSTRS_H
- #include "FWStrs.h"
- #endif
-
- #ifndef FWFILESP_H
- #include "FWFileSp.h"
- #endif
-
- #ifndef FWRESFIL_H
- #include "FWResFil.h"
- #endif
-
- #ifndef FWEXCDEF_H
- #include "FWExcDef.h"
- #endif
-
- #ifndef FWPRIDEB_H
- #include "FWPriDeb.h"
- #endif
-
- #ifndef FWBNDSTR_H
- #include "FWBndStr.h"
- #endif
-
- //----------------------------------------------------------------------------------------
- // Mac-only
- //----------------------------------------------------------------------------------------
- #ifdef FW_BUILD_MAC
-
- #pragma segment FWResource_PrivateResourceFile
-
- #ifndef __ERRORS__
- #include <errors.h>
- #endif
-
- #ifndef __ICONS__
- #include <Icons.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- #ifndef __STRING__
- #include <string.h>
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #endif
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- //========================================================================================
- // CLASS FW_PPrivResourceFile
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_PPrivResourceFile::FW_PPrivResourceFile
- //----------------------------------------------------------------------------------------
-
- FW_PPrivResourceFile::FW_PPrivResourceFile() :
- FW_CCountedPtr()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PPrivResourceFile::FW_PPrivResourceFile
- //----------------------------------------------------------------------------------------
-
- FW_PPrivResourceFile::FW_PPrivResourceFile(FW_CPrivResourceFileRep* privResourceFileRep) :
- FW_CCountedPtr(privResourceFileRep)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PPrivResourceFile::FW_PPrivResourceFile
- //----------------------------------------------------------------------------------------
-
- FW_PPrivResourceFile::FW_PPrivResourceFile(const FW_PPrivResourceFile& privResourceFile) :
- FW_CCountedPtr(privResourceFile)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PPrivResourceFile::~FW_PPrivResourceFile
- //----------------------------------------------------------------------------------------
-
- FW_PPrivResourceFile::~FW_PPrivResourceFile()
- {
- }
-
- //========================================================================================
- // CLASS FW_CPrivResourceFileRep
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceFileRep::FW_CPrivResourceFileRep
- //----------------------------------------------------------------------------------------
-
- FW_CPrivResourceFileRep::FW_CPrivResourceFileRep(FW_ResourceFileID resFileID) :
- FW_CCountedPtrRep(),
- fMustCloseFile(FALSE),
- fResourceFileID(resFileID),
- fFileSpec(FW_CPrivResourceFileRep::GetFileSpecificationForID(resFileID))
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceFileRep::FW_CPrivResourceFileRep
- //----------------------------------------------------------------------------------------
-
- FW_CPrivResourceFileRep::FW_CPrivResourceFileRep(const FW_CFileSpecification& fileSpec) :
- FW_CCountedPtrRep(),
- fMustCloseFile(TRUE),
- fResourceFileID(FW_CPrivResourceFileRep::OpenResourceFile(fileSpec)),
- fFileSpec(fileSpec)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceFileRep::~FW_CPrivResourceFileRep
- //----------------------------------------------------------------------------------------
-
- FW_CPrivResourceFileRep::~FW_CPrivResourceFileRep()
- {
- if (fMustCloseFile)
- FW_CPrivResourceFileRep::CloseResourceFile(fResourceFileID);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceFileRep::GetResource
- //
- // Private method. Attempts to load the resource, but will return NULL on failure.
- //----------------------------------------------------------------------------------------
-
- FW_ResourceHandle FW_CPrivResourceFileRep::GetResource(
- FW_ResourceId resourceId,
- FW_ResourceType resourceType) const
- {
- FW_ResourceHandle resourceHandle = NULL;
-
- #if defined FW_BUILD_WIN
- resourceHandle = ::FindResource(fResourceFileID,
- MAKEINTRESOURCE(resourceId),
- MAKEINTRESOURCE(resourceType));
-
- if(resourceHandle == 0)
- {
- char fileName[255] = {0};
- ::GetModuleFileName(fResourceFileID, fileName, sizeof fileName);
- __asm int 3
- }
-
- #elif defined FW_BUILD_MAC
- // Load the resource.
- short temp = CurResFile();
- if (temp!=fResourceFileID) ::UseResFile(fResourceFileID);
- resourceHandle = ::Get1Resource(resourceType, short(resourceId)); // Must cast!
- if (temp!=fResourceFileID) ::UseResFile(temp);
- #endif
-
- return resourceHandle;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceFileRep::GetResourceHandle
- //----------------------------------------------------------------------------------------
-
- FW_ResourceHandle FW_CPrivResourceFileRep::GetResourceHandle(
- FW_ResourceId resourceId,
- FW_ResourceType resourceType) const
- {
- FW_ResourceHandle handle = GetResource(resourceId, resourceType);
-
- // Throw an exception if the resource was not loaded.
- ThrowExceptionIfResourceLoadError(handle, resourceId, resourceType);
-
- return handle;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceFileRep::ReleaseResourceHandle
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivResourceFileRep::ReleaseResourceHandle(FW_ResourceHandle handle) const
- {
- #ifdef FW_BUILD_MAC
- ::ReleaseResource(handle);
- #else
- FW_UNUSED(handle);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceFileRep::GetFileSpecification
- //----------------------------------------------------------------------------------------
-
- const FW_CFileSpecification* FW_CPrivResourceFileRep::GetFileSpecification() const
- {
- return (&fFileSpec);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceFileRep::ThrowExceptionIfResourceLoadError
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivResourceFileRep::ThrowExceptionIfResourceLoadError(
- FW_PlatformHandle resourceHandle,
- FW_ResourceId resourceId,
- FW_ResourceType resourceType) const
- {
- if (resourceHandle != NULL)
- return;
-
- #ifdef FW_BUILD_MAC
- // Get the resource fError... BEFORE DOING ANYTHING ELSE!
- short resError = ::ResError();
- if (resError == noErr) resError = resNotFound;
- FW_Failure(resNotFound);
- #endif
-
- // Throw an exception
- #ifdef FW_BUILD_WIN
- if (!HasResource(resourceId, resourceType))
- FW_Failure(FW_xResourceNotFound);
- FW_Failure(FW_xResourceNotLoaded);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceFileRep::HasResource
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CPrivResourceFileRep::HasResource(FW_ResourceId resourceId,
- FW_ResourceType resourceType) const
- {
- #ifdef FW_BUILD_WIN
- FW_ResourceHandle resourceHandle;
- resourceHandle = ::FindResource(fResourceFileID,
- MAKEINTRESOURCE(resourceId),
- MAKEINTRESOURCE(resourceType));
- #endif
-
- #ifdef FW_BUILD_MAC
- FW_CMacResLoadFalse dontLoadResource;
- FW_PlatformHandle resourceHandle = GetResource(resourceId, resourceType);
- #endif
-
- return (resourceHandle != NULL);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceFileRep::GetFileSpecificationForID
- //----------------------------------------------------------------------------------------
-
- FW_CFileSpecification FW_CPrivResourceFileRep::GetFileSpecificationForID(FW_ResourceFileID resFileID)
- {
- #ifdef FW_BUILD_WIN
- // Get file name from Windows
- FW_Char buffer[255];
- ::GetModuleFileName(resFileID, buffer, sizeof buffer);
- FW_CString255 fileName(buffer);
- #elif defined FW_BUILD_MAC
- FW_PascalChar buffer[255];
- FCBPBRec pb;
- pb.ioCompletion = 0;
- pb.ioFCBIndx = 0;
- pb.ioVRefNum = 0;
- pb.ioRefNum = resFileID;
- pb.ioNamePtr = buffer;
- OSErr err = ::PBGetFCBInfoSync(&pb);
- if (err != noErr)
- buffer[0] = 0;
- FW_CString255 fileName((FW_Char *) buffer+1, buffer[0]);
- #endif
-
- // Return by value (copy constructor or operator= will be called)
- return FW_CFileSpecification(fileName);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceFileRep::OpenResourceFile
- //----------------------------------------------------------------------------------------
-
- FW_ResourceFileID FW_CPrivResourceFileRep::OpenResourceFile(const FW_CFileSpecification& fileSpec)
- {
- #ifdef FW_BUILD_WIN
- FW_CDynamicString fileName;
- fileSpec.GetFullPath(fileName);
- FW_ResourceFileID result = ::LoadLibrary(fileName);
-
- #ifdef FW_BUILD_WIN16
- if (result < HINSTANCE_ERROR)
- {
- int error = result;
- #endif
- #ifdef FW_BUILD_WIN32
- if(result == NULL)
- {
- DWORD error = ::GetLastError();
- #endif
- switch (error)
- {
- case 2: // File not found.
- case 3: // Path not found.
- FW_Failure(FW_xResourceFileNotFound);
- case 8: // Insufficient memory
- FW_Failure(FW_xMemoryExhausted);
- default:
- FW_Failure(FW_xResourceFile);
- }
- #ifdef FW_BUILD_WIN32
- }
- #endif
- #ifdef FW_BUILD_WIN16
- }
- #endif
- #endif
-
- #ifdef FW_BUILD_MAC
- FSSpec macSpec;
- fileSpec.MacGetFSSpec(macSpec);
- FW_ResourceFileID result = ::FSpOpenResFile(&macSpec, fsRdPerm);
- if (result == -1)
- {
- short resError = ::ResError();
- if (resError == noErr)
- resError = resFNotFound;
- FW_Failure(resError);
- }
- #endif
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceFileRep::CloseResourceFile
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivResourceFileRep::CloseResourceFile(FW_ResourceFileID file)
- {
- #if defined FW_BUILD_MAC
- ::CloseResFile(file);
- #elif defined FW_BUILD_WIN
- ::FreeLibrary(file);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceFileRep::PrivHasSpecialResource
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CPrivResourceFileRep::PrivHasSpecialResource(FW_ResourceId resourceId,
- FW_ResourceType resourceType) const
- {
- // (JEL) I can't find any reason why this function should be different from
- // HasResource. For now, I'll just call HasResource, but maybe later I'll
- // have to do something else.
- return HasResource(resourceId, resourceType);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivResourceFileRep::PrivGetSpecialResource
- //----------------------------------------------------------------------------------------
-
- FW_PlatformHandle
- FW_CPrivResourceFileRep::PrivGetSpecialResource(FW_ResourceId resourceId,
- FW_ResourceType resourceType) const
- {
- FW_PlatformHandle handle = NULL;
-
- #if defined FW_BUILD_MAC
- // (JEL) ??? Some resource types are truly "special", in that calling GetResource
- // to load them is a bad idea. However, several types of resoures have their
- // own GetXxxx toolbox call that is supposedly equivalent to just calling
- // GetResource(XXXX, id). For some of these types, we call the GetXxxx function,
- // but we allow the default handler to get the rest of these types. It bothers
- // me a little that we're not consistent, but it seems like there is no good
- // reason to worry about it.
- switch(resourceType)
- {
- case FW_kCursor:
- handle = (FW_PlatformHandle) ::GetCCursor(resourceId);
- break;
-
- case FW_kIcon:
- handle = (FW_PlatformHandle) ::GetCIcon(resourceId);
- break;
-
- case FW_kBlackWhiteIcon:
- handle = ::GetIcon(resourceId);
- break;
-
- case FW_kBlackWhiteCursor:
- handle = (FW_PlatformHandle) ::GetCursor(resourceId);
- break;
-
- case FW_kPicture:
- handle = (FW_PlatformHandle) ::GetPicture(resourceId);
- break;
-
- case FW_kBitmap:
- handle = ::GetResource(resourceType, resourceId);
- break;
-
- default:
- FW_DEBUG_MESSAGE("Not a special resource type");
- }
- #elif defined FW_BUILD_WIN
- switch(resourceType)
- {
- case FW_kCursor:
- handle = ::LoadCursor(fResourceFileID, MAKEINTRESOURCE(resourceId));
- break;
-
- case FW_kIcon:
- handle = ::LoadIcon(fResourceFileID, MAKEINTRESOURCE(resourceId));
- break;
-
- case FW_kBitmap:
- handle = ::LoadBitmap(fResourceFileID, MAKEINTRESOURCE(resourceId));
- break;
-
- case FW_kAccelerator:
- handle = ::LoadAccelerators(fResourceFileID, MAKEINTRESOURCE(resourceId));
- break;
-
- default:
- FW_DEBUG_MESSAGE("Not a special resource type");
- }
- #endif
-
- ThrowExceptionIfResourceLoadError(handle, resourceId, resourceType);
- return handle;
- }
-
-